# -*- coding: utf-8 -*-

'''
   Copyright (c) 2023. China Mobile(SuZhou)Software Technology Co.,Ltd. All rights reserved.
   secScanner is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2.
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
   EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
   MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
   See the Mulan PSL v2 for more details.
'''


import logging
import os
import re
from secScanner.gconfig import *
from secScanner.lib import *
logger = logging.getLogger("secscanner")


def C26_setaliases():
    InsertSection("check ls and rm aliases")
    set_ls = 'unset'
    set_rm = 'unset'
    if os.path.exists('/root/.bashrc'):
        with open('/root/.bashrc', 'r') as file:
            lines = file.readlines()
            for line in lines:
                if re.match('alias ls', line) and not re.match('^#|^$', line):
                    set_ls = 'set'
                    temp = line.strip('\n').split('=')
                    if len(temp) == 2 and temp[1] == '\'ls -al\'':
                        set_ls = 'right'

        with open('/root/.bashrc', 'r') as file:
            lines = file.readlines()
            for line in lines:
                if re.match('alias rm', line) and not re.match('^#|^$', line):
                    set_rm = 'set'
                    temp = line.strip('\n').split('=')
                    if len(temp) == 2 and temp[1] == '\'rm -i\'':
                        set_rm = 'right'

        if set_ls == 'unset' and set_rm == 'unset':
            with open(RESULT_FILE, "a") as file:
                file.write("\nC26\n")
            logger.warning("WRN_C26_01: %s", WRN_C26_01)
            logger.warning("SUG_C26: %s", SUG_C26)
            Display("- No ls and rm aliases set...", "WARNING")
        elif set_ls == 'right' and set_rm == 'right':
            logger.info("Has ls and rm aliases set, checking OK")
            Display("- Check the ls and rm aliases set...", "OK")
        else:
            with open(RESULT_FILE, "a") as file:
                file.write("\nC26\n")
            logger.warning("WRN_C26_02: %s", WRN_C26_02)
            logger.warning("SUG_C26: %s", SUG_C26)
            Display("- Wrong ls and rm aliases set...", "WARNING")
    else:
        Display("- file '/root/.bashrc' does not exist...", "SKIPPED")
